using System.Text.Json;
using System.Text.Json.Serialization;

namespace IMClient.Model.Message
{

    public abstract class BaseMessage
    {
        public string MessageId { get; set; } = null!;
        public string SenderId { get; set; } = null!;
        public string SenderName { get; set; } = null!;
        public string? SenderAvatar { get; set; } = null!;
        public string ReceiverId { get; set; } = null!;
        public string Content { get; set; } = null!;
        public bool IsRead { get; set; } = false;
        public bool IsSentByUser { get; set; }
        public abstract string MessageType { get; set; }
        public DateTime Timestamp { get; set; } = DateTime.UtcNow;

        // 序列化方法
        public virtual string ToJson()
        {
            var options = new JsonSerializerOptions
            {
                WriteIndented = false,
                DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
                Converters = { new JsonStringEnumConverter() }
            };

            return JsonSerializer.Serialize(this, GetType(), options);
        }

    }
}
